package xyz.viewmodel;

import android.app.Application;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.List;

import xyz.entity.Note;
import xyz.repo.NoteRepo;
import xyz.test.EditNoteActivity;

public class NoteViewModel extends AndroidViewModel {
    private NoteRepo mNoteRepo;
    private LiveData<List<Note>> mNoteLiveData;
    private LiveData<Note> mOneNoteLiveData;

    public NoteViewModel(Application application) {
        super(application);

        mNoteRepo = new NoteRepo(application);
    }

    public LiveData<List<Note>> getNoteLiveData() {
        if (mNoteLiveData == null) {
            mNoteLiveData = mNoteRepo.getAllNote();
        }
        return mNoteLiveData;

    }

    public LiveData<Note> getOneNoteLiveData(int id) {
        if (mNoteLiveData == null) {
            mOneNoteLiveData = mNoteRepo.getNoteById(id);
        }
        return mOneNoteLiveData;
    }

    public void addNote(Note note) {
        mNoteRepo.addNote(note);
    }

    public void updateNote(Note note, EditNoteActivity.OnUpdatedListener listener) {
        mNoteRepo.updateNote(note, listener);
    }

    public void delete(Note note) {
        mNoteRepo.deleteNote(note);
    }
}
